//
//  CustomNavView.swift
//  IdleFish
//
//  Created by Ben on 2021/12/14.
//

import SwiftUI

typealias CustomNavViewBackActionBlock = () -> ()

struct CustomNavView<Content>: View where Content: View {
    
    var backImageName: String = ""
    var backSystemImageName: String = ""
    var title: String = ""
    var content: (() -> Content)?
    var backAction: CustomNavViewBackActionBlock
    
    
    init(backSystemImageName: String = "chevron.backward", @ViewBuilder content: @escaping () -> Content, backAction: @escaping CustomNavViewBackActionBlock) {
        
        self.backSystemImageName = backSystemImageName
        self.content = content
        self.backAction = backAction
    }
    
    init(backImageName: String, @ViewBuilder content: @escaping () -> Content, backAction: @escaping CustomNavViewBackActionBlock) {
        
        self.backImageName = backImageName
        self.content = content
        self.backAction = backAction
    }
    
    var body: some View {
        HStack {
            
            if backSystemImageName.isEmpty {
                Button(action: backAction) {
                    Image(backImageName)
                }
            } else {
                Button(action: backAction) {
                    Image(systemName: backSystemImageName)
                }
            }
            
            
            Spacer()
            
            if title.isEmpty {
                content?()
            } else {
                Text("设置")
            }

            Spacer()
        }
        .font(.system(size: 14))
        .foregroundColor(.black)
        .padding(.horizontal, 16)
    }
}

extension CustomNavView where Content == Text {
    
    init(backSystemImageName: String = "chevron.backward", title: String, backAction: @escaping CustomNavViewBackActionBlock) {
        
        self.backSystemImageName = backSystemImageName
        self.title = title
        self.backAction = backAction
    }
    
    init(backImageName: String, title: String, backAction: @escaping CustomNavViewBackActionBlock) {
        
        self.backSystemImageName = backImageName
        self.title = title
        self.backAction = backAction
    }
}

//struct CustomNavView_Previews: PreviewProvider {
//    static var previews: some View {
//        CustomNavView(title: "") {
//
//        }
//    }
//}
